<?php

namespace App\Admin\Metrics\Examples;

use App\Models\Member;
use Carbon\Carbon;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NewUsers extends Line
{
    /**
     * 
     * 最近新增用户
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title('新增用户');
        $this->dropdown([
            '7' => 'Last 7 Days',
            // '28' => 'Last 28 Days',
            '30' => 'Last Month',
            '365' => 'Last Year',
        ]);
        $this->chartOption('type','line');
      
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
      
        
        switch ($request->get('option')) {
            case '365':
                $res = $this->getTimingHistory(365);
               // 卡片内容
               $this->withContent($res['sum'].'人');
               // 图表数据
               $this->withChart($res['nums']);
                break;
            case '30':
                $res = $this->getTimingHistory(30);
                // 卡片内容
                $this->withContent($res['sum'].'人');
                // 图表数据
                $this->withChart($res['nums']);
                break;
            // case '28':
            //     // 卡片内容
            //     $this->withContent(mt_rand(400, 1000).'k');
            //     // 图表数据
            //     $this->withChart(collect($generator(28))->toArray());
            //     break;
            case '7':
            default:
                $res = $this->getTimingHistory(7);
                // 卡片内容
                $this->withContent($res['sum'].'人');
                // 图表数据
                $this->withChart($res['nums']);
        }
    }

    public function getTimingHistory($time)
    {
        $time = (int)$time;
        $data = Member::where('add_time','<', strtotime(Carbon::now()->toDateTimeString()))
                ->where('add_time','>', $time > 1 ? strtotime(Carbon::today()->subDays($time)) : strtotime(Carbon::today()))
                ->select([$time > 1 ? DB::raw('FROM_UNIXTIME(add_time,\'%e\') as time') : DB::raw('DATE_FORMAT(add_time,\'%H\') as time'), DB::raw('COUNT("*") as count')])
                ->groupBy('time')
                ->get()->toArray();

        $day = $time ;
      
        for ($i = $day - 1; 0 <= $i; $i--) {
            $result[] = date('d', strtotime('-' . $i . ' day'));
            $nums[] = 0;
            $total[] = 0;
        }
       


        array_walk($data, function ($value, $key) use ($result, &$nums,&$total) {

            $index = array_search($value['time'],$result);
            $nums[$index] = $value['count'];
            
        });
       
        $data = [
            'day' => $result,
            'nums' => $nums,
        ];
        $data['sum'] = array_sum($data['nums']);
        
        // dd($data);
        return $data;
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
